package com.example.administrator.im.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.example.administrator.im.utils.DisplayUtils;

/**
 * create:2018/11/8 19:40
 * author: 姚鸿杰
 **/
public class MyHeadImage extends android.support.v7.widget.AppCompatImageView {
    public MyHeadImage(Context context) {
        this(context, null);
    }

    public MyHeadImage(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyHeadImage(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    private Context mContext;
    private int width, height;

    private Paint paint, shaderPaint;
    private Path path, shaderPath;

    private final int top = Color.argb(204, 76, 76, 76), in = Color.argb(0, 248, 248, 248);

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.argb(255, 248, 248, 248));

        path = new Path();
        path.moveTo(0, height);
        path.lineTo(width, height - DisplayUtils.dip2px(mContext, 85));
        path.lineTo(width, height);
        path.close();

        shaderPath = new Path();
        shaderPath.moveTo(0, 0);
        shaderPath.lineTo(width, 0);
        shaderPath.lineTo(width, height - DisplayUtils.dip2px(mContext, 85));
        shaderPath.lineTo(0, height);
        shaderPath.close();

        shaderPaint = new Paint();
        Shader shader = new LinearGradient(0, height / 3, 0, height, in, top
                , Shader.TileMode.MIRROR);
        shaderPaint.setShader(shader);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        init();
        canvas.drawPath(path, paint);
        canvas.drawPath(shaderPath, shaderPaint);
    }
}
